package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.db.DaoUtils;
import com.shaoshaohuo.app.entity.EcCategoryThird;
import com.shaoshaohuo.app.entity.MyPriceEntity.MyPrice;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.StringHelper;

import java.util.List;

/**
 * 收到的报价  4 供应收到司机报价 5 采购收到供应的报价
 */
public class MyPriceReceivedAdapter extends MyBaseAdapter<MyPrice> {

    public MyPriceReceivedAdapter(Context ctx, List<MyPrice> list, boolean flag) {
        super(ctx, list, flag);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if ("5".equals(list.get(position).state)) {
            return 1;
        }
        return 0;
    }

    @Override
    public View getView(int pos, View view, ViewGroup group) {

        if (0 == getItemViewType(pos)) {
            if (view == null) {
                view = View.inflate(context, R.layout.item_my_price_1, null);
            }

            MyPrice myPrice = list.get(pos);
            ViewHolder holder = ViewHolder.getHolder(view);
            holder.ll_price_set.setVisibility(View.GONE);
            holder.ll_price.setVisibility(View.GONE);
            holder.b_go_price.setVisibility(View.GONE);

            holder.tv_name.setText(myPrice.ordername);
            holder.tv_num.setText(myPrice.shippweight);
            holder.tv_start_time.setText(myPrice.origintime);
            holder.tv_start_ad.setText(AreaUtil.getAreaFullname(myPrice.origincityid)+myPrice.originaddress);
            holder.tv_end_ad.setText(AreaUtil.getAreaFullname(myPrice.receivecityid)+myPrice.receiveaddress);
            holder.tv_end_time.setText(myPrice.receivetime);
//            holder.tv_received_price_num.setText(myPrice.num);

        } else if (1 == getItemViewType(pos)) {
            if (view == null) {
                view = View.inflate(context, R.layout.item_my_price_2, null);
            }

            MyPrice myPrice = list.get(pos);
            ViewHolderCar holder = ViewHolderCar.getHolder(view);

            EcCategoryThird ecCategoryThird = DaoUtils.findEcCategoryThird(myPrice.catid);
            if (ecCategoryThird != null) {
//                EcCategorySecond ecCategorySecond = DaoUtils.findEcCategorySecond(ecCategoryThird.getPid());
//                holder.tv_name.setText(ecCategorySecond.getCatname());
//                holder.tv_cat.setText(ecCategoryThird.getCatname());
            }

            holder.tv_time.setText(StringHelper.formatDateMinute(myPrice.ctime));
            holder.tv_num.setText(myPrice.num);
            holder.tv_num_unit.setText(myPrice.unit);

            if(TextUtils.isEmpty(myPrice.start)||"0".equals(myPrice.start)){
                holder.tv_start_time.setText("--");
            }else{
                holder.tv_start_time.setText(StringHelper.formatDateDay(myPrice.start));
            }

            if (TextUtils.isEmpty(myPrice.end)||"0".equals(myPrice.end)) {
                holder.tv_end_time.setText("--");
            }else{
                holder.tv_end_time.setText(StringHelper.formatDateDay(myPrice.end));
            }

            holder.tv_money.setText(myPrice.money);
            holder.tv_money_unit.setText("/" + myPrice.unit);
        }


        return view;
    }

    static class ViewHolderCar {

        public TextView tv_name;
        public TextView tv_cat;
        public TextView tv_time;
        public TextView tv_num;
        public TextView tv_num_unit;
        public TextView tv_start_time;
        public TextView tv_end_time;
        public TextView tv_money;
        public TextView tv_money_unit;

        private ViewHolderCar(View view) {
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_cat = (TextView) view.findViewById(R.id.tv_cat);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
            tv_num_unit = (TextView) view.findViewById(R.id.tv_num_unit);
            tv_start_time = (TextView) view.findViewById(R.id.tv_start_time);
            tv_end_time = (TextView) view.findViewById(R.id.tv_end_time);
            tv_money = (TextView) view.findViewById(R.id.tv_money);
            tv_money_unit = (TextView) view.findViewById(R.id.tv_money_unit);
            view.setTag(this);
        }

        public static ViewHolderCar getHolder(View convertView) {
            return (ViewHolderCar) (convertView.getTag() != null ? convertView.getTag() : new ViewHolderCar(convertView));
        }
    }

    static class ViewHolder {

        public TextView tv_name;
        public TextView tv_num;
        public TextView tv_start_time;
        public TextView tv_start_ad;
        public TextView tv_end_ad;
        public TextView tv_end_time;
        public TextView b_go_price;
        public LinearLayout ll_price;
        public TextView tv_price;
        public LinearLayout ll_price_set;
        public TextView b_modify_price;
        public TextView b_delete_price;
//        public LinearLayout ll_received_price;
//        public TextView tv_received_price_num;


        private ViewHolder(View view) {
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
            tv_start_time = (TextView) view.findViewById(R.id.tv_start_time);
            tv_start_ad = (TextView) view.findViewById(R.id.tv_start_ad);
            tv_end_ad = (TextView) view.findViewById(R.id.tv_end_ad);
            tv_end_time = (TextView) view.findViewById(R.id.tv_end_time);

            b_go_price = (TextView) view.findViewById(R.id.b_go_price); // 1 待报价

            ll_price = (LinearLayout) view.findViewById(R.id.ll_price);// 2 已提交的报价
            tv_price = (TextView) view.findViewById(R.id.tv_price);
            ll_price_set = (LinearLayout) view.findViewById(R.id.ll_price_set);
            b_modify_price = (TextView) view.findViewById(R.id.b_modify_price);
            b_delete_price = (TextView) view.findViewById(R.id.b_delete_price);

//            ll_received_price = (LinearLayout) view.findViewById(R.id.ll_received_price);// 5 已收到的报价
//            tv_received_price_num = (TextView) view.findViewById(R.id.tv_received_price_num);

            view.setTag(this);
        }

        public static ViewHolder getHolder(View convertView) {
            return (ViewHolder) (convertView.getTag() != null ? convertView.getTag() : new ViewHolder(convertView));
        }
    }
}
